import json
import os
import sys
from common.content_type import ContentType
from common.time_bdp import time_bdp
from core.result_base import ResultBase
from api.ds import ds
from common.logger import logger
from common.user_agent import useragent
import urllib3
urllib3.disable_warnings()

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(BASE_PATH)



def ds_nslist(access_token,_t=time_bdp.time_stamp_13(),_xsrf=""):
    """
    获取数据源列表
    :param _t: 13位的时间戳
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": _t,
        "_xsrf":_xsrf
    }
    headers = {
        "User-Agent": useragent.get_user_agent_one(),
        "Cookie":"access_token={}".format(access_token)
  }

    res = ds.nslist(params=payload, headers=headers)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("获取数据源列表 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



def ds_all(access_token,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    获取所有的可添加的数据源
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = ds.all(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("获取所有的可添加的数据源 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



def ds_category(access_token,db_type,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    添加数据源
    :param db_type :数据源类型
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "db_type":db_type,
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = ds.category(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("添加数据源 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def tb_delete_batch(access_token,tb_list,verify_code,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    批量删除工作表
    :param tb_id :工作表ID
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "tb_list": json.dumps([tb_list]),
        "verify_code":verify_code,
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = tb.delete_batch(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("批量删除工作表 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def tb_preview(access_token,tb_id,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    浏览工作表
    :param tb_id :工作表ID
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "tb_id": tb_id,
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = tb.preview(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("浏览工作表 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def tb_chart_check(access_token,tb_id,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    检查工作表是否关联了图表
    :param tb_id :工作表ID
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "tb_id": tb_id,
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = tb.chart_check(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("检查工作表是否关联了图表 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def tb_list_check_rely(access_token,tb_id,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    浏览工作表
    :param tb_id :工作表ID
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "tb_id": tb_id,
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = tb.preview(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("浏览工作表 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



def tb_status(access_token,tb_id,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    获取工作表状态
    :param tb_id :工作表ID
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "tb_id": tb_id,
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = tb.status(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("获取工作表状态 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



def tb_info(access_token,tb_id,_t=time_bdp.time_stamp_13(),_xsrf=None):
    """
    获取工作表信息
    :param tb_id :工作表ID
    :param _t :
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "tb_id": tb_id,
        "_t":_t,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }

    res = tb.info(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("获取工作表信息 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result

# def project_modify(access_token,_t,name,proj_id,_xsrf):
#     """
#     编辑文件夹
#     :param _t: 13位的时间戳
#     :param name: 文件夹名称
#     :param proj_id : 要修改的文件夹id
#     :param _xsrf :
#     :return: 自定义的关键字返回结果 result
#     """
#     result = ResultBase()
#     payload = {
#         "_t": _t,
#         "data":{"name": name},
#         "proj_id": proj_id,
#         "_xsrf":_xsrf
#     }
#
#     header = {
#         "Content-Type": "application/json;charset=utf-8"
#    }
#
#     res = project.modify(access_token,json=payload, headers=header)
#     result.success = False
#     if res.json()["status"] == "0":
#         result.success = True
#     else:
#         result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
#     result.payload = res.json()
#     result.response = res
#     logger.info("修改文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
#     return result


# def folder_newtree(access_token,_t,_xsrf,tree_type,tb_id,folder_list):
#     """
#     获取工作表的所有文件夹
#     :param _t: 13位的时间戳
#     :param _xsrf :
#     :param tree_type :0是我的，1是授权的
#     :param tb_id :
#     :param folder_list :
#     :return: 自定义的关键字返回结果 result
#     """
#     result = ResultBase()
#     payload = {
#         "_t": _t,
#         "tree_type": tree_type,
#         "tb_id":tb_id,
#         "_xsrf": _xsrf,
#         "folder_list": folder_list
#     }
#
#     headers = {
#         "Content-Type": ContentType.APPLICATION_JSON.value,
#         "User-Agent": useragent.get_user_agent_one()
#     }
#
#     cookies = {
#         "access_token":access_token
#     }
#
#
#     res = folder.newtree(params=payload, headers=headers,cookies=cookies)
#     result.success = False
#     if res.json()["status"] == "0":
#         result.success = True
#     else:
#         result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
#     result.payload = res.json()
#     result.response = res
#     logger.info("获取工作表所有文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
#     return result








